//
//  ApiClient.m
//  GiftTalk
//
//  Created by justaway on 15/8/16.
//  Copyright (c) 2015年 swiftyper. All rights reserved.
//

#import <AFNetworking.h>
#import <MJExtension.h>
#import "ApiClient.h"
#import "Gift.h"
#import "AppConstants.h"

@implementation ApiClient

+ (ApiClient *)sharedInstance {
    static ApiClient *_sharedInstance = nil;
    
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[ApiClient alloc] init];
    });
    
    return _sharedInstance;
}

- (void)getGiftsWithGender:(GenderType)gender GenerationType:(GenerationType)generation completion:(void (^)(NSArray *gifts, NSError *error))completion {
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *requestPath = [ApiBaseURL stringByAppendingPathComponent:@"v2/items"];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"gender"] = @(gender);
    parameters[@"generation"] = @(generation);
    
    [manager GET:requestPath parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        
        NSDictionary *data = responseObject[@"data"];
        NSArray *items = data[@"items"];
        
        NSMutableArray *gifts = [NSMutableArray array];
        for (NSDictionary *itemDict in items) {
            Gift *gift = [Gift objectWithKeyValues:itemDict[@"data"]];
            [gifts addObject:gift];
        }
        
        !completion ? : completion(gifts, nil);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        !completion ? : completion(nil, error);
    }];
    
    
}


@end
